﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.IO;

namespace SlaktDataReader.Wins
{
    /// <summary>
    /// Interaction logic for winBook.xaml
    /// </summary>
    public partial class winBook : Window, INotifyPropertyChanged
    {
        public Book bk { get; set; }
        public bool GotoBook { get; set; }
        public bool HideGoto { get; set; }

        public ObservableCollection<Location> Counties
        {
            get
            {
                if (Globals.Locations != null)
                {
                    return new ObservableCollection<Location>((from v in Globals.Locations where v.Level > 1 orderby v.LocationName select v).ToList());
                }
                return null;
            }
        }

        public bool IsBirthBook { get { return bk.BookType == BookTypes.Born ? true : false; } set { bk.BookType = BookTypes.Born; _NotifyTypes(); } }
        public bool IsDeathBook { get { return bk.BookType == BookTypes.Dead ? true : false; } set { bk.BookType = BookTypes.Dead; _NotifyTypes(); } }
        public bool IsMarriageBook { get { return bk.BookType == BookTypes.Marriages ? true : false; } set { bk.BookType = BookTypes.Marriages; _NotifyTypes(); } }
        public bool IsMoveInBook { get { return bk.BookType == BookTypes.MoveIn ? true : false; } set { bk.BookType = BookTypes.MoveIn; _NotifyTypes(); } }
        public bool IsMoveOutBook { get { return bk.BookType == BookTypes.MoveOut ? true : false; } set { bk.BookType = BookTypes.MoveOut; _NotifyTypes(); } }
        public bool IsHouseCallBook { get { return bk.BookType == BookTypes.HouseCall ? true : false; } set { bk.BookType = BookTypes.HouseCall; _NotifyTypes(); } }
        public bool IsBoUBook { get { return bk.BookType == BookTypes.BoU ? true : false; } set { bk.BookType = BookTypes.BoU; _NotifyTypes(); } }
        public bool IsJuridicalBook { get { return bk.BookType == BookTypes.Juridical ? true : false; } set { bk.BookType = BookTypes.Juridical; _NotifyTypes(); } }

        public string BookCode
        {
            get
            {
                switch (bk.BookType)
                {
                    case BookTypes.Born:
                        return "B";
                    case BookTypes.Dead:
                        return "D";
                    case BookTypes.Marriages:
                        return "V";
                    case BookTypes.MoveIn:
                        return "I";
                    case BookTypes.MoveOut:
                        return "U";
                    case BookTypes.HouseCall:
                        return "Ai";
                    case BookTypes.BoU:
                        return "Bo";
                    case BookTypes.Juridical:
                        return "B";

                    default:
                        return "";
                }
            }
        }

        public Brush BookColor
        {
            get
            {
                switch (bk.BookType)
                {
                    case BookTypes.Born:
                        return (Brush)FindResource("BookColor2");
                    case BookTypes.Dead:
                        return (Brush)FindResource("BookColor1");
                    case BookTypes.Marriages:
                        return (Brush)FindResource("BookColor3");
                    case BookTypes.MoveIn:
                        return (Brush)FindResource("BookColor5");
                    case BookTypes.MoveOut:
                        return (Brush)FindResource("BookColor4");
                    case BookTypes.HouseCall:
                        return (Brush)FindResource("BookColor6");
                    case BookTypes.BoU:
                        return new SolidColorBrush(Colors.DarkGray);
                    case BookTypes.Juridical:
                        return new SolidColorBrush(Colors.Gray);
                    default:
                        return new SolidColorBrush(Colors.Transparent);
                }
            }
        }

        public winBook()
        {
            bk = new Book();
            //bk.BookType = BookTypes.Dead;

            InitializeComponent();

            GotoBook = false;
        }

        private void Notify(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Notify("bk");
            _NotifyTypes();

        }

        public void OpenFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;



            if (!string.IsNullOrEmpty(bk.FileName))
            {
                if (Directory.Exists(bk.FileName))
                    dlg.InitialDirectory = System.IO.Path.GetDirectoryName(bk.FileName);

                dlg.FileName = bk.FileName;
            }

            if (dlg.ShowDialog() == true)
            {
                bk.FileName = dlg.FileName;
                Notify("bk");
            }
        }

        private void _NotifyTypes()
        {
            Notify("IsBirthBook");
            Notify("IsDeathBook");
            Notify("IsMarriageBook");
            Notify("IsMoveInBook");
            Notify("IsMoveOutBook");
            Notify("IsHouseCallBook");
            Notify("IsBoUBook");
            Notify("IsJuridicalBook");

            Notify("BookCode");
            Notify("BookColor");

            switch (bk.BookType)
            {
                case BookTypes.Dead:
                case BookTypes.Born:
                case BookTypes.Marriages:
                case BookTypes.HouseCall:
                case BookTypes.MoveOut:
                    bGoto.IsEnabled = true;
                    break;
                default:
                    bGoto.IsEnabled = false;
                    break;
            }
        }

        private void bGoto_Click(object sender, RoutedEventArgs e)
        {
            if (bk.BookType == BookTypes.Dead)
            {
                GotoBook = true;
                this.DialogResult = true;
                this.Close();
            }
        }

        private void bClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void bOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void bFindFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFile();
        }
    }
}
